//
//  LHConnection.m
//  GameKitDemo
//
//  Created by Xiaobin Li on 7/18/15.
//  Copyright (c) 2015 Xiaobin Li. All rights reserved.
//

#import "LHConnection.h"

@implementation LHConnection

- (void)dealloc
{
}

#pragma mark - Public methods

- (NSString *)otherPeerName
{
    return [self.chatSession displayNameForPeer:self.remotePeerID];
}

- (void)connect
{
    // Overridden by subclasses
}

- (void)disconnect
{
    // Overridden by subclasses
}

- (void)answerIncomingCall
{
    // Overridden by subclasses
}

- (void)denyIncomingCall
{
    // Overridden by subclasses
}

- (GKSession *)createChatSession
{
    NSString *sessionId = @"bluewoki";
    NSString *name = [[UIDevice currentDevice] name];
    GKSession* session = [[GKSession alloc] initWithSessionID:sessionId
                                                   displayName:name
                                                   sessionMode:GKSessionModePeer];
    return session;
}

#pragma mark - GKVoiceChatClient methods

- (NSString *)participantID
{
    return self.chatSession.peerID;
}

- (void)voiceChatService:(GKVoiceChatService *)voiceChatService
                sendData:(NSData *)data
         toParticipantID:(NSString *)participantID
{
    [self.chatSession sendData:data
                       toPeers:[NSArray arrayWithObject:participantID]
                  withDataMode:GKSendDataReliable
                         error:nil];
}

- (void)receiveData:(NSData *)data
           fromPeer:(NSString *)peer
          inSession:(GKSession *)session
            context:(void *)context;
{
    [[GKVoiceChatService defaultVoiceChatService] receivedData:data
                                             fromParticipantID:peer];
}

- (void)voiceChatService:(GKVoiceChatService *)voiceChatService
didStopWithParticipantID:(NSString *)participantID
                   error:(NSError *)error
{
    self.connected = NO;
    if ([self.delegate respondsToSelector:@selector(connectionDidDisconnect:)])
    {
        [self.delegate connectionDidDisconnect:self];
    }
}

- (void)voiceChatService:(GKVoiceChatService *)voiceChatService
didStartWithParticipantID:(NSString *)participantID
{
}

@end
